package com.sf.fy.uav.applet.controller.operate;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.sf.fy.uav.common.enums.*;
import com.sf.fy.uav.common.request.operate.SysPromotionRecordsRequest;
import com.sf.fy.uav.common.response.operational.SysActivityResponse;
import com.sf.fy.uav.common.response.operational.SysPromotionRecordsResponse;
import com.sf.fy.uav.common.response.operational.SysPromotionStatisticsResponse;
import com.sf.fy.uav.common.response.order.BillingResponse;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.rest.PromotionRecordsRest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author LTQ
 * @since 2023-08-16
 */
@RestController
@RequestMapping("/home/promotion-records")
@Tag(name = "推广记录管理", description = "推广记录管理")
public class SysPromotionRecordsController {
    private final static Logger logger = LoggerFactory.getLogger(SysPromotionRecordsController.class);

    @Autowired
    private PromotionRecordsRest promotionRecordsRest;


    @GetMapping("get-promotion-records")
    @Operation(tags = "获取推广记录", description = "获取推广记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "payerPhoneNumber", value = "下单用户手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "promotionPhoneNumber", value = "推广用户手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tcWaybillNo", value = "订单编号", dataType = "String", paramType = "query")
    })
    @CrossOrigin
    @Parameter(name = "page", description = "页码")
    @Parameter(name = "size", description = "单页条数")
    @Parameter(name = "payerPhoneNumber", description = "下单用户")
    @Parameter(name = "promotionPhoneNumber", description = "推广用户")
    @Parameter(name = "tcWaybillNo", description = "订单编号")
    @Parameter(name = "activityId", description = "活动id")
    public Rs<List<SysPromotionRecordsResponse>> getPromotionRecords(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String payerPhoneNumber, @RequestParam(required = false) String promotionPhoneNumber,
                                                                     @RequestParam(required = false) String tcWaybillNo, @RequestParam(required = false) String activityId) {

        SysPromotionRecordsRequest sysPromotionRecordsRequest = new SysPromotionRecordsRequest();
        sysPromotionRecordsRequest.setPage(page);
        sysPromotionRecordsRequest.setSize(size);
        sysPromotionRecordsRequest.setPayerPhoneNumber(payerPhoneNumber);
        sysPromotionRecordsRequest.setPromotionPhoneNumber(promotionPhoneNumber);
        sysPromotionRecordsRequest.setTcWaybillNo(tcWaybillNo);
        sysPromotionRecordsRequest.setActivityId(activityId);
        return Rs.success(promotionRecordsRest.getPromotionRecords(sysPromotionRecordsRequest));
    }
    @GetMapping("export")
    @ApiOperation(value = "导出", notes = "/export")
    public void export(HttpServletResponse response,@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String payerPhoneNumber, @RequestParam(required = false) String promotionPhoneNumber, @RequestParam(required = false) String tcWaybillNo, @RequestParam(required = false) String platfoactivityId) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=推广订单明细表-" + sdf.format(new Date()) + ".xls");
            ExportParams exportParams = new ExportParams();
            Rs<List<SysPromotionRecordsResponse>> listRs = this.getPromotionRecords(page, size, payerPhoneNumber, promotionPhoneNumber, tcWaybillNo, platfoactivityId);
            DateFormat fat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SysPromotionRecordsResponse sysPromotionRecordsResponse: listRs.getData()) {
                if(sysPromotionRecordsResponse.getOrderTime()!=null){
                    sysPromotionRecordsResponse.setOrderDate(fat.format(sysPromotionRecordsResponse.getOrderTime()));
                }
                if(sysPromotionRecordsResponse.getFinishTime()!=null){
                    sysPromotionRecordsResponse.setFinishDate(fat.format(sysPromotionRecordsResponse.getFinishTime()));
                }
                if(sysPromotionRecordsResponse.getPromotionReward()!=null){
                    sysPromotionRecordsResponse.setPromotionReward(sysPromotionRecordsResponse.getPromotionReward().setScale(2, RoundingMode.HALF_UP));
                }
            }
            List<SysPromotionRecordsResponse> sysPromotionRecordsResponseList = listRs.getData();
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SysPromotionRecordsResponse.class, sysPromotionRecordsResponseList);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @GetMapping("get-total")
    @CrossOrigin
    @Operation(tags = "获取推广记录条数", description = "获取推广记录条数")
    @Parameter(name = "payerPhoneNumber", description = "下单用户")
    @Parameter(name = "promotionPhoneNumber", description = "推广用户")
    @Parameter(name = "tcWaybillNo", description = "订单编号")
    @Parameter(name = "activityId", description = "活动id")
    public Rs<Long> getTotal(@RequestParam(required = false) String payerPhoneNumber, @RequestParam(required = false) String promotionPhoneNumber,
                             @RequestParam(required = false) String tcWaybillNo, @RequestParam(required = false) String activityId) {
        return Rs.success(promotionRecordsRest.getTotal(payerPhoneNumber, promotionPhoneNumber, tcWaybillNo, activityId));
    }


}
